﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using QuTouTiaoAssist.Util;

namespace QuTouTiaoAssist
{
    public partial class FrmExchangeProduct : FrmListViewBase
    {
        public FrmExchangeProduct()
        {
            InitializeComponent();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (!base.CheckLogin())
                return;

            new Thread(() =>
            {
                try
                {
                    base.ClearListView(this.listView1);
                    var result = QttHelper.GetExchangeProduct(base.MemeberToken);
                    result.Data = result.Data.OrderBy(match => match.Name).ToList();
                    for (int i = 0; i < result.Data.Count; i++)
                    {
                        var info = result.Data[i];
                        base.AppendListViewItem(this.listView1, new object[]
                        {
                            i+1,
                            info.Id,
                            info.Name,
                            info.DiscountPrice
                        });
                    }
                }
                catch (Exception ex)
                {
                    MsgBox.ShowInfo("获取失败，" + ex.Message);
                }
            }){IsBackground = true}.Start();
        }

        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
                return;
            if (this.listView1.SelectedItems.Count != 1)
                return;

            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Items.Add("兑换", null, (o, args) =>
            {
                if (!base.CheckLogin())
                    return;

                var selectedItem = this.listView1.SelectedItems[0];
                string itemId = selectedItem.SubItems[1].Text;
                string itemName = selectedItem.SubItems[2].Text;
                ExchangeItem(itemId, itemName);
            });
            cms.Show(this.listView1, e.Location);
        }

        private void ExchangeItem(string itemId, string itemName)
        {
            if (MsgBox.ShowYesNo("是否兑换商品：" + itemName + "?") == DialogResult.Yes)
            {
                try
                {
                    var result = QttHelper.ExchangeItem(base.MemeberToken, itemId);
                    if (result.Code == 0)
                    {
                        MsgBox.ShowInfo("兑换成功！");
                    }
                    else
                    {
                        throw new Exception(result.Message);
                    }
                }
                catch (Exception ex)
                {
                    MsgBox.ShowInfo("兑换失败，" + ex.Message);
                }
            }
        }
    }
}
